package it.polimi.swim.session;

import java.util.List;

import it.polimi.swim.entity.Feedback;
import it.polimi.swim.entity.HelpRequest;
import it.polimi.swim.entity.SystemUser;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Session Bean implementation class FeedbackManagerBean
 */
@Stateless
public class FeedbackManagerBean implements FeedbackManagerBeanRemote {

	@PersistenceContext(unitName = "SWIMv2")
	private EntityManager em;
    public FeedbackManagerBean() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public Feedback createFeedback(Feedback feedback) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Feedback associateToUser(Feedback feedback, SystemUser user) {
		return null;
	}

	@Override
	public void createFeedback(String feed, String feedMess, int idHelp) {
		Feedback f = new Feedback();
		f.setText(feedMess);
		if(feed!=null){
			if(feed.compareTo("OK")==0) f.setEvaluation(true);
			else f.setEvaluation(false);
		}
		Query q = em.createNamedQuery("HelpRequest.getHelpRequestById");
		q.setParameter("id", idHelp);
		HelpRequest help = (HelpRequest) q.getSingleResult();
		help.setFeedback(f);
		f.SetHelp(help);
		em.persist(f);
		em.persist(help);
	}

	@Override
	public List<Feedback> getFeedbackByUser(SystemUser user) {
		Query q = em.createNamedQuery("Feedback.getFeedbackByUser");
		q.setParameter("id", user);
		List<Feedback> feed = q.getResultList();
		return feed;
	}

}
